package com.sg.service.biz.component.nb;

import cn.hutool.core.bean.BeanUtil;
import com.sg.dto.biz.component.common.AnalyzeDataSetCompletionDto;
import com.sg.dto.biz.component.req.*;
import com.sg.dto.biz.component.res.*;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ： 手工接入方法
 * @version 1.0
 * @since 2022/5/28 15:32
 */
@Service
public class NbDivineData {
    /**
     * code:receptionService
     * name:M2接收输出参数
     * desc:undefined
     * gen by moon at 8/24/2022, 1:54:13 PM
     **/
    @Trace(operationName = "M2接收输出参数")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public QueryOutputParametersDetailRespDto queryOutputParametersDetail(QueryOutputParametersDetailReqDto reqDto) {
        return BeanUtil.toBean(reqDto, QueryOutputParametersDetailRespDto.class);
    }

    /**
     * code:getLastCycleTypeCode
     * name:M2获取上一周期类型(特殊方法)
     * desc:undefined
     * gen by moon at 8/28/2022, 4:03:07 PM
     **/
    @Trace(operationName = "M2获取上一周期类型(特殊方法)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ObtainLastCycleTypeRespDto obtainLastCycleType(ObtainLastCycleTypeReqDto reqDto) {
        ObtainLastCycleTypeRespDto retData = new ObtainLastCycleTypeRespDto();
        List<CycleTypeDto> filteredList = new ArrayList<>(reqDto.getCycleTypeList());
        Collections.sort(filteredList, new Comparator<CycleTypeDto>() {
            @Override
            public int compare(CycleTypeDto o1, CycleTypeDto o2) {
                if (o1.getOrderNumber() < o2.getOrderNumber()) {
                    return 0;
                } else {
                    return 1;
                }
            }
        });
        CycleTypeDto last = new CycleTypeDto();
        for (CycleTypeDto oneCycle : filteredList) {
            if (oneCycle.getCycleType().equals(reqDto.getCurrentCycleType())) {
                retData.setResultCycleType(last.getCycleType());
            } else {
                last = oneCycle;
            }
        }
        return retData;
    }

    /**
     * code:timeEfficiencyCheck
     * name:M2检查时间范围（特殊方法）
     * desc:undefined
     * gen by moon at 8/28/2022, 4:03:16 PM
     **/
    @Trace(operationName = "M2检查时间范围（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public CheckTimeScopeSearchRespDto checkTimeScopeSearch(CheckTimeScopeSearchReqDto reqDto) {
        CheckTimeScopeSearchRespDto retData = new CheckTimeScopeSearchRespDto();
        if (reqDto.getCurrentTime().compareTo(reqDto.getCompareTime()) < 0) {
            retData.setTimeCompareResult("BEFORE");
        } else if (reqDto.getCurrentTime().compareTo(reqDto.getCompareTime()) == 0) {
            retData.setTimeCompareResult("PRESENT");
        } else if (reqDto.getCurrentTime().compareTo(reqDto.getCompareTime()) > 0) {
            retData.setTimeCompareResult("LATER");
        }
        return retData;
    }

    /**
     * code:collectionsMergeData
     * name:M2执行多数据集取并集（特殊方法）
     * desc:undefined
     * gen by moon at 12/25/2022, 6:43:05 PM
     **/
    @Trace(operationName = "M2执行多数据集取并集（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementUnionMultipleDataSetsRespDto implementUnionMultipleDataSets(ImplementUnionMultipleDataSetsReqDto reqDto) {
        ImplementUnionMultipleDataSetsRespDto retData = new ImplementUnionMultipleDataSetsRespDto();
        // TODO: 2023/2/8 手写代码，单转多
        retData.getUnionMultipleDataSetsList().addAll(reqDto.getDataSetsListOne().stream().map(data -> BeanUtil.toBean(data, UnionMultipleDataSetsDto.class)).collect(Collectors.toList()));
        retData.getUnionMultipleDataSetsList().addAll(reqDto.getDataSetsListTwo().stream().map(data -> BeanUtil.toBean(data, UnionMultipleDataSetsDto.class)).collect(Collectors.toList()));
        return retData;
    }

    /**
     * code:analyzeDataSetCompletion
     * name:M2执行分析数据集完成情况（特殊方法）
     * desc:undefined
     * gen by moon at 12/25/2022, 9:25:40 PM
     **/
    @Trace(operationName = "M2执行分析数据集完成情况（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ImplementAnalyzeDataSetCompletionRespDto implementAnalyzeDataSetCompletion(ImplementAnalyzeDataSetCompletionReqDto reqDto) {
        ImplementAnalyzeDataSetCompletionRespDto retData = new ImplementAnalyzeDataSetCompletionRespDto();
        Long count = 0L;
        for (AnalyzeDataSetCompletionDto one : reqDto.getAnalyzeDataSetCompletionList()) {
            if (one.getIsComplete() != null && one.getIsComplete().equals("TRUE")) {
                count++;
            }
        }
        retData.setComFloatField1(count.doubleValue());
        retData.setComFloatField2(Double.valueOf(reqDto.getAnalyzeDataSetCompletionList().size()));
        if (count.longValue() == reqDto.getAnalyzeDataSetCompletionList().size()) {
            retData.setIsComplete("TRUE");
        } else {
            retData.setIsComplete("FALSE");
        }
        return retData;
    }

    /**
     * code:maxOfMultipleNumAddOne
     * name:M2-获取数据集最大值加一（特殊方法）
     * desc:undefined
     * gen by moon at 6/2/2024, 12:25:01 AM
     **/
    @Trace(operationName = "M2-获取数据集最大值加一（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ObtainMaxOfCollectionsAddOneRespDto obtainMaxOfCollectionsAddOne(ObtainMaxOfCollectionsAddOneReqDto reqDto) {
        ObtainMaxOfCollectionsAddOneRespDto retData = new ObtainMaxOfCollectionsAddOneRespDto();
        if (reqDto.getMaxOfMultipleNumList() == null || reqDto.getMaxOfMultipleNumList().size() == 0) {
            retData.setOrderNumber(1L);
            return retData;
        }
        retData.setOrderNumber(Collections.max(reqDto.getMaxOfMultipleNumList()) + 1);
        return retData;
    }
    //手工接入方法
}
